<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="searchParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="印章名称" prop="name">
        <el-input v-model="searchParams.name" placeholder="请输入印章名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="20" class="mb20">
      <el-col :span="6">
        <el-button type="primary" plain icon="el-icon-plus" size="small"
          @click="handleAdd">新建印章</el-button>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="table.loading" :data="table.data">
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="印章名称" prop="name" min-width="200" />
      <el-table-column label="印章文件" align="center" prop="url" min-width="160px">
        <template v-slot="{ row }">
          <image-preview :src="row.url" :width="'100px'"></image-preview>
        </template>
      </el-table-column>
      <!-- <el-table-column label="印章状态" align="center" prop="status">
        <template v-slot="{ row }">
          <el-tag :type="getStatusType(row.status)" size="small" effect="plain">
            {{ itemStatusMap[row.status] || row.status }}
          </el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template v-slot="{ row }">
          <el-button type="text" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="text" @click="remove(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="table.total > 0" :total="table.total" :page.sync="table.page.pageNo"
      :limit.sync="table.page.pageSize" @pagination="getList" />

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="save.title" :visible.sync="save.open" width="500px" append-to-body>
      <el-form ref="form" :model="save.form" :rules="save.rules" label-width="80px">
        <el-form-item label="印章名称" prop="name">
          <el-input v-model="save.form.name" placeholder="请输入印章名称" clearable />
        </el-form-item>
        <el-form-item label="印章文件" size="normal" prop="url">
          <image-upload v-model="save.form.url" :limit="1" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="save.form.remark" placeholder="请输入印章备注" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { DICT_TYPE, getDictDatas } from "@/utils/dict";
import { getSealList, deleteSealInfo, saveSealInfo } from "@/api/system/seal";
import ImageUpload from "@/components/ImageUpload";
import ImagePreview from "@/components/ImagePreview";

export default {
  components: { ImageUpload, ImagePreview },
  data() {
    return {
      searchParams: {
        name: "",
        // status: "",
      },
      table: {
        data: [],
        loading: false,
        total: 0,
        page: {
          pageNo: 1,
          pageSize: 10,
        },
      },
      save: {
        title: "",
        open: false,
        form: {
          id: undefined,
          name: undefined,
          remark: undefined,
          url: undefined,
        },
        rules: {
          name: [
            {
              required: true,
              message: "请输入印章名称",
              trigger: "change",
              type: "string",
            },
          ],
          url: [
            {
              required: true,
              message: "请上传印章",
              trigger: "change",
              type: "string",
            },
          ],
        },
      },
    };
  },
  created() {
    this.handleQuery();
  },
  methods: {
    handleQuery() {
      this.table.page.pageNo = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.table.loading = true;
      // 执行查询
      getSealList({ ...this.searchParams, ...this.table.page })
        .then((res) => {
          const { list, total } = res?.data || {};
          this.table.data = list || [];
          this.table.total = total || 0;
        })
        .finally(() => {
          this.table.loading = false;
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.save.open = true;
      this.save.title = "添加菜单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if (row) {
        const { id, name, remark, url } = row;
        this.save.form = { id, name, remark, url };
      }
      this.save.open = true;
      this.save.title = "修改菜单";
    },
    getStatusType(status) {
      return (
        {
          1: "info",
          2: "warning",
          3: "success",
          // 4: "danger",
        }[status] || "info"
      );
    },
    remove({ id }) {
      this.$confirm("该操作不可恢复，是否删除该印章？", "删除印章", {
        type: "warning",
      })
        .then(() => {
          return deleteSealInfo(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
    },
    submitForm() {
      saveSealInfo(this.save.form).then(({ code }) => {
        if (!code) {
          this.save.open = false;
          this.$message.success("创建成功");
          this.getList();
        }
      });
    },
    // 取消按钮
    cancel() {
      this.save.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.save.form = {
        id: undefined,
        name: undefined,
        remark: undefined,
        url: undefined,
      };
      this.resetForm("form");
    },
  },
};
</script>

<style>
</style>
